package cn.edu.jsu.nrs.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cn.edu.jsu.nrs.dao.UserDao;
import cn.edu.jsu.nrs.model.User;
@Component(value="userService")
public class UserService {
	private UserDao userDao;
	
	
	
	/****
	 * 添加用户信息
	 * @param user
	 * @return
	 */
	public boolean addUser(User user){
		int result=userDao.save(user);
		if (result>0) {
			return true;
		}
		return false;
	}
	/**
	 * 更新用户
	 * @param user
	 */
	public void updateUser(User user){
		userDao.update(user);
	}
	
	/****
	 * 通过用户id查找用户
	 * @param id
	 * @return
	 */
	public User findUserById(int id){
		return userDao.queryUserById(id);
	}
	
	/****
	 * 查询所有 用户
	 * @return
	 */
	public List<User> getAllUsers(){
		return userDao.queryAllUsers();
	}
	public void deleteUser(User user){
		userDao.delete(user);
	}

	public List<User> getUserList(final int beginIndex,final int count,String userName,final String sord,String sidx){
		return userDao.queryUserList(beginIndex, count, userName, sord, sidx);
	}
	
	/****
	 * 通过用户id删除用户
	 * @param id 
	 */
	public void deleteUserById(int id){
		userDao.deleteById(id);
		
	}
	
	/****
	 * 判断用户是否存在
	 * @param email
	 * @return 
	 */
	public boolean isExistsUser(String userName){
		if (userDao.isExists(userName)) {
			return true;
		}
		return false;
	}
	
	public User doLogin(String userName,String password,int isAdmin){
		return 	userDao.queryUser(userName, password,isAdmin);
	}

	/**
	 * 分页查询用户列表
	 * @param hql
	 * @param beginIndex
	 * @param legth
	 * @return
	 */
	public List<User> getListForPage(final String hql,final int beginIndex,final int legth){
		return userDao.getListForPage(hql, beginIndex, legth);
	}
	
	public int getUsersCounts(final String userName,final String sord,final String sidx){
		return userDao.queryUsersCounts(userName);
	}
	
	/**
	 * 获得用户记录数
	 * @return
	 */
	public int getUsersCounts(){
		return userDao.queryUsersCounts();//查询用户记录数
	}
	
	
	@Resource(name="userDaoImpl")
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
}
